package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.netease.nim.avchatkit.common.util.TimeUtil;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.CalendarView;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/7/18 14:20
 * 功能：
 */
public class RegisterSettingActivity extends BaseActivity {

    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.relative1)
    RelativeLayout relative1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.relative2)
    RelativeLayout relative2;
    @BindView(R.id.text3)
    EditText text3;
    @BindView(R.id.relative3)
    RelativeLayout relative3;

    private String userdays = "";//会员日
    private String value;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register_setting;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        getConfig();
    }


    public void alert_label(String text) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        LayoutInflater factory = LayoutInflater.from(this);//提示框
        final View view = factory.inflate(R.layout.dialog_edit, null);//这里必须是final的
        final EditText et = view.findViewById(R.id.editText);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);
        et.setHint(text);
        builder.setTitle(text);

        final AlertDialog dialog = builder.create();
        dialog.setView(view);

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        });
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(et.getText().toString())) {

                    value = et.getText().toString();
                    config("regfee", et.getText().toString());
                    dialog.dismiss();

                } else {

                    Toast("非法输入!");

                }


            }
        });


    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:


                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        JSONObject object = jsonObject.getJSONObject("data");
                        text1.setText(object.getString("regfee") + "元");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
                case 2:


                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        Toast(jsonObject.getString("msg"));

                        text1.setText(value + "元");


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    public void config(String name, String value) {

        List<String> l = new ArrayList<>();
        l.add(value);

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "setConfig");
        params.put("data[" + name + "]", value);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 2, 404);

    }

    public void dialog() {

        LayoutInflater inflater = LayoutInflater.from(this);//获取一个填充器
        View view = inflater.inflate(R.layout.dialog_membersday, null);//填充我们自定义的布局
        Display display = getWindowManager().getDefaultDisplay();//得到当前屏幕的显示器对象
        Point size = new Point();//创建一个Point点对象用来接收屏幕尺寸信息
        display.getSize(size);//Point点对象接收当前设备屏幕尺寸信息
        int width = size.x;//从Point点对象中获取屏幕的宽度(单位像素)
        int height = size.y;//从Point点对象中获取屏幕的高度(单位像素)
        //创建一个PopupWindow对象，第二个参数是设置宽度的，用刚刚获取到的屏幕宽度乘以2/3，取该屏幕的2/3宽度，从而在任何设备中都可以适配，高度则包裹内容即可，最后一个参数是设置得到焦点
        final PopupWindow popWindow = new PopupWindow(view, 4 * width / 5, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());//设置PopupWindow的背景为一个空的Drawable对象，如果不设置这个，那么PopupWindow弹出后就无法退出了
        popWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow
        WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象
        params.alpha = 0.8f;//设置参数的透明度为0.8，透明度取值为0~1，1为完全不透明，0为完全透明，因为android中默认的屏幕颜色都是纯黑色的，所以如果设置为1，那么背景将都是黑色，设置为0，背景显示我们的当前界面
        getWindow().setAttributes(params);//把该参数对象设置进当前界面中
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {//设置PopupWindow退出监听器
            @Override
            public void onDismiss() {//如果PopupWindow消失了，即退出了，那么触发该事件，然后把当前界面的透明度设置为不透明
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                getWindow().setAttributes(params);
            }
        });
        //第一个参数为父View对象，即PopupWindow所在的父控件对象，第二个参数为它的重心，后面两个分别为x轴和y轴的偏移量
        popWindow.showAtLocation(inflater.inflate(R.layout.activity_register_setting, null), Gravity.CENTER, 0, 0);

        final CalendarView mCalendarView = view.findViewById(R.id.calendarView);

        //设置显示已经设置的会员日
        List<String> mDatas = new ArrayList<>();
        if (!TextUtils.isEmpty(userdays)) {

            boolean status = userdays.contains(",");

            //先判断是否有没有选择了多天
            if (status) {

                String[] result1 = userdays.split(",");
                for (int i = 0; i < result1.length; i++) {

                    //再判断是否是个位数
                    if (result1[i].length() > 1) {

                        mDatas.add(TimeUtil.DateConversionDay(new Date()) + result1[i]);

                    } else {

                        mDatas.add(TimeUtil.DateConversionDay(new Date()) + "0" + result1[i]);

                    }

                }

            } else {

                //再判断是否是个位数
                if (userdays.length() > 1) {

                    mDatas.add(TimeUtil.DateConversionDay(new Date()) + userdays);

                } else {

                    mDatas.add(TimeUtil.DateConversionDay(new Date()) + "0" + userdays);

                }

            }

        }

        for (int i = 0; i < mDatas.size(); i++) {

            Log.e("数据", "" + mDatas.get(i).toString());

        }

        // 设置可选日期
        mCalendarView.setSelectDate(mDatas);
        mCalendarView.setChangeDateStatus(true);
        mCalendarView.setOnDataClickListener(new CalendarView.OnDataClickListener() {
            @Override
            public void onDataClick(@NonNull CalendarView view, int year, int month, int day) {

                Log.e("test", "year: " + year);
                Log.e("test", "month,: " + (month + 1));
                Log.e("test", "day: " + day);

            }
        });

        mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, boolean select, int year, int month, int day) {
                if (select) {

                    Toast.makeText(mActivity
                            , "选中了" + day + "日为出诊日", Toast.LENGTH_SHORT).show();
                }
            }
        });


        Button submit = view.findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                List<String> s = mCalendarView.getSelectDate();
                StringBuffer buf = new StringBuffer();
                StringBuffer bufs = new StringBuffer();

                if (s.size() > 0) {

                    for (int j = 0; j < s.size(); j++) {

                        buf.append(TimeUtil.getDay(s.get(j)) + ",");
                        bufs.append(s.get(j) + ",");

                    }

                    userdays = bufs.toString();
                    if(!TextUtils.isEmpty(userdays)){

                        userdays = userdays.substring(0,userdays.length() - 1);

                    }
                    text2.setText(buf.toString());

                } else {
                    userdays = "";
                }

                Log.e("选择时间","选择userdays时间"+userdays);
//                DeleteCard("userdays",userdays,2);


                //修改

                popWindow.dismiss();

            }
        });

        Button cancel = view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });


    }

    @OnClick({R.id.relative1, R.id.relative2,R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.relative1:
                alert_label("挂号价格");
                break;
            case R.id.relative2:
                dialog();
                break;
            case R.id.submit:

                if (text3.getText().toString().equals("0")){
                    Toast("请输入号数");
                    return;
                }
                if (userdays.equals("")){
                    Toast("请选择时间");
                    return;
                }
                setVisit();
                break;
        }
    }


    public void getConfig() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getConfig");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);

    }



    //请求网络
    public void setVisit() {

        List<String> ls = new ArrayList<>();
        ls.add(text3.getText().toString());
        ls.add(userdays);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "setVisit");
        params.put("data[showtime]", userdays);
        params.put("data[opens]", text3.getText().toString());
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.Register/entry", params, handler, 2, 404);

    }


}
